#!/usr/bin/python
#encoding:utf-8


import os
import time
import datetime


def rsync_log(source_dir):
    cmd = r'rsync -avL {}* root@10.104.90.58::gamelogs --timeout=60 --password-file=/etc/rsyncd.passwd'.format(source_dir)
    os.system(cmd)


def backup_log(file_list, backup_dir):
    for item in file_list :
        os.system("\cp -rpf {}  {}".format(item, backup_dir))


def old_log(backup_dir):
    old_file_set = set()
    for p,d,f in os.walk(backup_dir):
        for file_name  in f:
            old_file_set.add(file_name)
    return (old_file_set)

'''log_done()函数，从log_done.txt 文件获取曾经rsync推送到备份服务器的日志文件
'''
def log_done(cwd):
    path = cwd + 'log_done.txt'
    log_done_set = set()
    with open(path, 'r') as fd:
        for item in fd:
            log_done_set.add(item.strip())
    return (log_done_set)


'''保存已经rsync推送过的日志到 log_done.txt
'''
def save_log_done(cwd, log_save_list):
    path = cwd + 'log_done.txt'
    with open(path, 'a+')  as fd:
        for item in log_save_list:
            fd.write(item.split("/")[2] + '\n')


'''清空备份目录
'''
def clear_backup(backup_dir):
    cmd = r'rm -rf {}*'.format(backup_dir)
    os.system(cmd)


def compare_time(file_list):
    result = []
    tmp_time = datetime.datetime.now()
    tmp_time_str = tmp_time.strftime("%y%m%d-%H")
    now_time = datetime.datetime.strptime(tmp_time_str, "%y%m%d-%H")
    for item in file_list:
        tt = item.split(".")[-1]
        if datetime.datetime.strptime(tt, "%y%m%d-%H")  <  now_time:
            result.append(item)
    return (result)


def  get_file(rootdir):
    result = []
    file_list = []
    for p,d,f  in os.walk(rootdir):
        for i in f:
            f_name = os.path.join(p, i)
            if os.path.isfile(f_name):
                file_list.append(f_name)
    new_list =  [item for item in file_list if len(item.split("."))==3 ]
    for item in new_list:
        tmp_list = item.strip().split(",")
        if tmp_list[0].startswith("/tmp/superserver"):
            result.append(item)
        elif tmp_list[0].startswith("/tmp/sessionserver"):
            result.append(item)
        elif tmp_list[0].startswith("/tmp/recordserver"):
            result.append(item)
        elif tmp_list[0].startswith("/tmp/sceneserver"):
            result.append(item)

    return (result)



if __name__ == '__main__' :
    rootdir = '/tmp/'
    backup_dir = '/home/ubuntu/server_20001/chen_yunwei/yunwei/log_backup/'
    cwd = '/home/ubuntu/server_20001/chen_yunwei/yunwei/'


    '''日志目录里的需要的四种类型的日志
    '''
    source_file_list = get_file(rootdir)

    '''符合时间条件的日志文件
    '''
    fetch_file_list = compare_time(source_file_list)
    print(len(fetch_file_list))

    log_done_set = set()
    log_done_set =  log_done(cwd)
    print(len(log_done_set))
    '''如果备份目录里没有的日志，则需要备份目录
    '''
    backup_log_list = []
    for item in fetch_file_list:
        if item.split("/")[2]  not in log_done_set:
            backup_log_list.append(item)
    print(len(backup_log_list))
    backup_log(backup_log_list, backup_dir)


    rsync_log(backup_dir)

    save_log_done(cwd, backup_log_list)

    clear_backup(backup_dir)